package com.itheima.health.service.impl;

import com.itheima.health.dao.ReportDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@DubboService
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;


    @Override
    public Map<String, Object> getBusinessReportData() {
        /*
        * @Date: 5:19 下午 2021/1/16
        * @name: getBusinessReportData
        * @param: []
        * @return: java.util.Map<java.lang.String,java.lang.Object>
        * @Description: 封装统计数据
        * 1、先通过calendar格式化日期
        * 2、编写sql获取对应的数据
        */
        //创建容器
        Map<String,Object> map = new HashMap<>();
        //转换日期
        Calendar today = Calendar.getInstance();
        //时分秒清零
        today.set(Calendar.HOUR_OF_DAY,0);
        today.set(Calendar.MINUTE,0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND,0);

        today.setFirstDayOfWeek(Calendar.MONDAY);
        //每周周一
        Calendar weekFirstDate = (Calendar)today.clone();
        weekFirstDate.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        //每周周末
        Calendar weekLastDate = (Calendar) today.clone();
        weekLastDate.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        //每月第一天
        Calendar monthFirstDate = (Calendar) today.clone();
        monthFirstDate.set(Calendar.DAY_OF_MONTH,1);
        //每月最后一天
         Calendar monthLastDate = (Calendar) today.clone();
        int monthMaxDate = monthLastDate.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDate.set(Calendar.DAY_OF_MONTH,monthMaxDate);


        //编写sql获取 数据
        //获取当天新增会员
        long todayNewMember = reportDao.countByRegTime(today.getTime(),today.getTime());
        //本周新增会员
        long thisWeekNewMember = reportDao.countByRegTime(weekFirstDate.getTime(),weekLastDate.getTime());
        //本月新增会员
        long thisMonthNewMember = reportDao.countByRegTime(monthFirstDate.getTime(),monthLastDate.getTime());
        //总会员
        long totalMember = reportDao.countMember();
        //今日预约数
        long todayOrderNumber = reportDao.countByOrderTimeOrStatus(null,today.getTime(),today.getTime());
        //本周预约数
        long thisWeekOrderNumber = reportDao.countByOrderTimeOrStatus(null,weekFirstDate.getTime(),weekLastDate.getTime());
        //本月预约数
        long thisMonthOrderNumber = reportDao.countByOrderTimeOrStatus(null,today.getTime(),today.getTime());
        //今日到珍数
        long todayVisitsNumber = reportDao.countByOrderTimeOrStatus(Order.ORDERSTATUS_YES,today.getTime(),today.getTime());
        //本周到珍数
        long thisWeekVisitsNumber = reportDao.countByOrderTimeOrStatus(Order.ORDERSTATUS_YES,weekFirstDate.getTime(),weekLastDate.getTime());
        //本月到珍数
        long thisMonthVisitsNumber = reportDao.countByOrderTimeOrStatus(Order.ORDERSTATUS_YES,monthFirstDate.getTime(),monthLastDate.getTime());
        //最热套餐

        List<Map<String,Object>> countByHotSetMeal = reportDao.countByHotSetMeal(4);

        map.put("reportDate",new SimpleDateFormat("yyyy-MM-dd").format(today.getTime()));
        map.put("todayNewMember",todayNewMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("totalMember",totalMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",countByHotSetMeal);

        log.info("[统计分析-套餐-返回数据] - map: {}",map);
        return map;
    }


}
